import 'package:flutter/material.dart';
enum OverlayAlign{
  LEFT,
  CENTER,
  RIGHT
}
///下拉ui
class FDropdown {
  static final FDropdown _singleton = FDropdown._internal();

  static FDropdown get instance => FDropdown();


  factory FDropdown() {
    return _singleton;
  }

  FDropdown._internal(){
    //做一些操作
  }

  OverlayEntry? _overlayEntry;
  bool _isShow = false;

  ///展示下拉ui
  void show({required GlobalKey distWidgetKey,required Widget child,bool? isSelfExpand,
    EdgeInsetsGeometry? allMargin,double? allMarginLeft,double? allMarginRight,double? allMarginTop,
    EdgeInsetsGeometry? contMargin,double? contMarginLeft,double? contMarginRight,double? contMarginTop,
    double? width,double? height
    ,Color? allBgColor,Color? contBgColor,Color? shadowColor,OverlayAlign? align,double? elevation,ShapeBorder? shape
  }){
    var buildContext = distWidgetKey.currentContext;
    if(buildContext==null){
      throw Exception('无效key值');
      return;
    }
    var findRenderObject = buildContext?.findRenderObject();
    final RenderBox? renderBox = (findRenderObject as RenderBox);
    final Offset? globalPosition = renderBox?.localToGlobal(Offset.zero);
    double? dy = globalPosition?.dy;
    Size? distSize = renderBox?.size;
    double? distSizeHeight = distSize?.height;

    _overlayEntry = OverlayEntry(builder: (_) {
      return Positioned(
          top: 0,
          bottom: 0,
          left: 0,
          right: 0,
          child: GestureDetector(
            onTap: (){
              _isShow = false;
              _overlayEntry?.remove();
            },
            child: Container(
              color: Colors.transparent,
              child: Container(
                color: allBgColor??Colors.transparent,
                width: MediaQuery.of(buildContext).size.width,
                height: MediaQuery.of(buildContext).size.height,
                margin: allMargin??EdgeInsets.only(
                  top: ((dy??0)+(distSizeHeight??0)+(allMarginTop??3)),
                  left: allMarginLeft??0,
                  right: allMarginRight??0,
                ),
                alignment: (align??OverlayAlign.RIGHT)==OverlayAlign.RIGHT?Alignment.topRight:
                ((align??OverlayAlign.RIGHT)==OverlayAlign.CENTER?Alignment.topCenter:Alignment.topLeft),
                child: UnconstrainedBox(
                  child: GestureDetector(
                    onTap: (){},
                    child:(isSelfExpand??false)?
                    Container(
                      width: MediaQuery.of(buildContext!).size.width,
                      constraints: BoxConstraints(
                        maxHeight: MediaQuery.of(buildContext!).size.height-((dy??0)+(distSizeHeight??0)+(allMarginTop??3))
                      ),
                      child: Card(
                          margin: contMargin??EdgeInsets.only(
                            top: contMarginTop??0,
                            left: contMarginLeft??0,
                            right: contMarginRight??0,
                          ),
                          elevation: elevation??5,
                          shadowColor: shadowColor??Color(0xffcccccc),
                          color: contBgColor??Colors.white,
                          shape: shape??RoundedRectangleBorder(),
                          child:child,
                      ),
                    ) :
                    Card(
                      margin: contMargin??EdgeInsets.only(
                        top: contMarginTop??0,
                        left: contMarginLeft??0,
                        right: contMarginRight??0,
                      ),
                      elevation: elevation??5,
                      shadowColor: shadowColor??Color(0xffcccccc),
                        color: contBgColor??Colors.white,
                      shape: shape??RoundedRectangleBorder(),
                      child:
                      Container(
                        width: width??(MediaQuery.of(buildContext!).size.width/2),
                        height: height??(MediaQuery.of(buildContext!).size.height/3),
                        child: child,
                      )
                    )
                  ),
                ),
              ),
            ),
          )
      );
    });
    Overlay.of(buildContext!).insert(_overlayEntry!);
    _isShow = true;
  }
  ///页面关闭时调用
  void dispose(){
    if(_isShow){
      _overlayEntry?.remove();
      _isShow = false;
    }
  }
}
